package com.fufan.mtsaas.modules.newtech.xjstechhis.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fufan.mtsaas.modules.base.log.service.TSysLogSvc;
import com.fufan.mtsaas.modules.common.bo.response.RequestPojo;
import com.fufan.mtsaas.modules.common.bo.response.ResStatus;
import com.fufan.mtsaas.modules.common.bo.response.Result;
import com.fufan.mtsaas.modules.newtech.xjstechhis.bo.request.XjsTechHisQueryRequest;
import com.fufan.mtsaas.modules.newtech.xjstechhis.bo.request.XjsTechHisSaveRequest;
import com.fufan.mtsaas.modules.newtech.xjstechhis.bo.response.XjsTechHisResponse;
import com.fufan.mtsaas.modules.newtech.xjstechhis.dao.XjsTechHisDao;
import com.fufan.mtsaas.modules.newtech.xjstechhis.dao.XjsTechHisYearAnnualDao;
import com.fufan.mtsaas.modules.newtech.xjstechhis.model.XjsTechHis;
import com.fufan.mtsaas.modules.newtech.xjstechhis.model.XjsTechHisYearAnnual;
import com.fufan.mtsaas.modules.newtech.xjstechhis.service.XjsTechHisSvc;
import com.fufan.mtsaas.modules.sysmgr.sydept.dao.SYDeptDao;
import com.fufan.mtsaas.modules.sysmgr.sydept.model.SYDept;
import com.fufan.mtsaas.modules.sysmgr.syuser.dao.SYuserDao;
import com.fufan.mtsaas.modules.sysmgr.syuser.model.SYuser;
import com.fufan.mtsaas.util.ParamUtil;
import com.github.pagehelper.ISelect;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 新技术历史维护表 服务实现类
 * </p>
 *
 * @author Liby
 * @since 2024-12-16
 */
@Service("XjsTechHisSvcImpl")
@Slf4j
@Primary
public class XjsTechHisSvcImpl extends ServiceImpl<XjsTechHisDao, XjsTechHis> implements XjsTechHisSvc {
    @Autowired
    private XjsTechHisDao entitymDao;

    @Autowired
    private TSysLogSvc tSysLogSvc;

    @Autowired
    private SYuserDao userDao;

    @Autowired
    private SYDeptDao deptDao;

    @Autowired
    private XjsTechHisYearAnnualDao xjsTechHisYearAnnualDao;

    /**
     * 查询
     *
     * @param map
     * @return Result.class
     */
    @Override
    public Result doQueryPage(XjsTechHisQueryRequest map) {
        Result<XjsTechHisResponse> result = new Result<>();

        Page<XjsTechHisResponse> page = new Page<>();
        long total = com.github.pagehelper.PageHelper.count(new ISelect() {
            @Override
            public void doSelect() {
                entitymDao.selectByPage(page, map);
            }
        });

        // 分页
        ParamUtil.setPage(map.getPageSize(), map.getCurrentPage(), page);
        //分页查询
        List<XjsTechHisResponse> list = entitymDao.selectByPage(page, map);

        Page<XjsTechHisYearAnnual> page1 = new Page<>();
        for (XjsTechHisResponse item : list) {
            XjsTechHisYearAnnual xjsTechHisYearAnnual = new XjsTechHisYearAnnual();
            xjsTechHisYearAnnual.setHisId(Long.valueOf(item.getId()));

            List<XjsTechHisYearAnnual> hislist = xjsTechHisYearAnnualDao.queryAll(xjsTechHisYearAnnual, page1);
            item.setAnnualHis(hislist);
        }

        page.setRecords(list);
        page.setTotal((int) total);
        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);
        result.setResData(page);
        return result;
    }

    /**
     * 新增
     *
     * @param entitym
     * @param requestPojo
     * @return Resulass
     */
    @Override
    @Transactional
    public Result doSave(XjsTechHisSaveRequest entitym, RequestPojo requestPojo) {
        Result result = new Result();

        entitym.setIsEnabled(1);
        entitym.setDelFlag(0);
        entitym.setCreateBy(requestPojo.getUserName());
        entitym.setUpdateBy(requestPojo.getUserName());
        entitym.setUpdateTime(new Date());
        entitym.setCreateTime(new Date());


        // 部门信息，页面提交数据
        if (null != entitym.getApplyDeptId()) {
            SYDept dept = deptDao.selectById(entitym.getApplyDeptId());
            if (null != dept) {
                if (dept.getManager() != 0) {
                    SYuser manager = userDao.selectById(dept.getManager());
                    if (null != manager) {
                        entitym.setApplyManagerId(Long.valueOf(String.valueOf(manager.getId())));
                        entitym.setApplyManagerName(manager.getName());
                    }
                }
            }
        }

        // 申请人
        if (null != entitym.getApplyUserId()) {
            SYuser manager = userDao.selectById(entitym.getApplyUserId());
            if (null != manager) {
                entitym.setApplyUserName(manager.getName());
            }
        }

        entitymDao.insert(entitym);

        for (XjsTechHisYearAnnual item : entitym.getAnnualHis()) {
            item.setId(0L);
            item.setHisId(Long.valueOf(entitym.getId()));
            item.setCreateBy(requestPojo.getUserName());
            item.setUpdateBy(requestPojo.getUserName());
            item.setUpdateTime(new Date());
            item.setCreateTime(new Date());
            xjsTechHisYearAnnualDao.insert(item);
        }

        tSysLogSvc.saveLogInfo(Long.valueOf(entitym.getId()), "xjshistory", "新增项目历史", "项目名称:" + entitym.getName(), "新增", "成功", requestPojo);

        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);
        return result;

    }

    /**
     * 修改
     *
     * @param entitym
     * @param requestPojo
     * @return Result.class
     */
    @Override
    @Transactional
    public Result doUpdate(XjsTechHisSaveRequest entitym, RequestPojo requestPojo) {
        Result result = new Result();

        entitym.setIsEnabled(1);
        entitym.setDelFlag(0);
        entitym.setUpdateBy(requestPojo.getUserName());
        entitym.setUpdateTime(new Date());

        // 部门信息，页面提交数据
        if (null != entitym.getApplyDeptId()) {
            SYDept dept = deptDao.selectById(entitym.getApplyDeptId());
            if (null != dept) {
                if (dept.getManager() != 0) {
                    SYuser manager = userDao.selectById(dept.getManager());
                    if (null != manager) {
                        entitym.setApplyManagerId(Long.valueOf(String.valueOf(manager.getId())));
                        entitym.setApplyManagerName(manager.getName());
                    }
                }
            }
        }

        // 申请人
        if (null != entitym.getApplyUserId()) {
            SYuser manager = userDao.selectById(entitym.getApplyUserId());
            if (null != manager) {
                entitym.setApplyUserName(manager.getName());
            }
        }

        entitymDao.updateById(entitym);

        xjsTechHisYearAnnualDao.deleteByHisId(entitym.getId());
        for (XjsTechHisYearAnnual item : entitym.getAnnualHis()) {
            item.setId(0L);
            item.setHisId(Long.valueOf(entitym.getId()));
            item.setCreateBy(entitym.getCreateBy());
            item.setUpdateBy(requestPojo.getUserName());
            item.setUpdateTime(new Date());
            item.setCreateTime(entitym.getCreateTime());
            xjsTechHisYearAnnualDao.insert(item);
        }

        tSysLogSvc.saveLogInfo(Long.valueOf(entitym.getId()), "xjshistory", "修改项目历史", "项目名称:" + entitym.getName(), "更新", "成功", requestPojo);

        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);

        return result;

    }

    /**
     * 删除
     *
     * @param map
     * @return Result.class
     */
    @Override
    @Transactional
    public Result doDelete(XjsTechHisQueryRequest map, RequestPojo requestPojo) {
        Result result = new Result();

        entitymDao.deleteById(map.getId());

        XjsTechHis xjsTechHis = entitymDao.selectById(map.getId());
        if (Objects.isNull(xjsTechHis)) {
            result.setResStatus(ResStatus.STATUS_DATABASE_FAIL);
            result.setResMsg(ResStatus.STATUS_UNAVAILABLE_MSG);
            return result;
        }

//        xjsTechHisYearAnnualDao.deleteByHisId(map.getId());

        tSysLogSvc.saveLogInfo(Long.valueOf(xjsTechHis.getId()), "xjshistory", "删除项目历史", "项目名称:" + xjsTechHis.getName(), "删除", "成功", requestPojo);

        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);

        return result;

    }
}
